<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
 lang="en" dir="ltr">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link rel="stylesheet" href="../codebase/default.css" type="text/css" media="screen" title="no title" charset="utf-8"><title>
    dhtmlxconnector:formatting_changing_data_before_loading    [Skin test wiki]
  </title></head>

<body>
<div class="dokuwiki">
  
  <div class="stylehead">

    <div class="header">
    <a class="logo" href="http://dhtmlx.com" title="DHTMLX Documentation homepage"></a>
    <span class="tittle-dhtmlx">DHTMLX Docs & Samples Explorer</span>

    <div class="search-field">
        <form action="http://dhtmlx.com/dhxdocs/doku.php" accept-charset="utf-8" id="dw__search"><div class="no"><input type="hidden" name="do" value="search" /><input type="text" id="qsearch__in" accesskey="f" name="id" title="Search" /><button type="submit"></button><div id="qsearch__out" class="ajax_qsearch JSpopup"></div></div></form>    </div>
        <div class="buttons">
                <a class="doc"></a>
                <a title="DHTMLX Samples homepage" href="../samples.html" class="sample"/></a>
        </div>
     <!-- <div class="pagename">
        [[<a href="#"  title="Backlinks">dhtmlxconnector:formatting_changing_data_before_loading</a>]]
      </div>
      <div class="logo">
        <a href="/dhtmlx/_lab/wiki/doku.php"  name="dokuwiki__top" id="dokuwiki__top" accesskey="h" title="[H]">Skin test wiki</a>      </div>

      <div class="clearer"></div>-->
        
    </div>

    
<!--   <div class="bar" id="bar__top">
      <div class="bar-left" id="bar__topleft">
                <form class="button btn_revs" method="get" action="http://dhtmlx.com/dhxdocs/doku.php"><div class="no"><input type="hidden" name="do" value="revisions" /><input type="hidden" name="id" value="dhtmlxconnector:formatting_changing_data_before_loading" /><input type="submit" value="Old revisions" class="button" accesskey="o" title="Old revisions [O]" /></div></form>      </div>

      <div class="bar-right" id="bar__topright">
        <form class="button btn_recent" method="get" action="http://dhtmlx.com/dhxdocs/doku.php"><div class="no"><input type="hidden" name="do" value="recent" /><input type="hidden" name="id" value="dhtmlxconnector:formatting_changing_data_before_loading" /><input type="submit" value="Recent changes" class="button" accesskey="r" title="Recent changes [R]" /></div></form>        <form action="http://dhtmlx.com/dhxdocs/doku.php" accept-charset="utf-8" id="dw__search"><div class="no"><input type="hidden" name="do" value="search" /><input type="text" id="qsearch__in" accesskey="f" name="id" title="Search" /><button type="submit"></button><div id="qsearch__out" class="ajax_qsearch JSpopup"></div></div></form>&nbsp;
      </div>

      <div class="clearer"></div>
    </div> -->

<!--        <div class="breadcrumbs">
      <span class="bchead">Trace:</span> <span class="bcsep">&raquo;</span> <span class="curid"><a href="/dhtmlx/_lab/wiki/doku.php?id=dhtmlxconnector:formatting_changing_data_before_loading"  class="breadcrumbs" title="dhtmlxconnector:formatting_changing_data_before_loading">formatting_changing_data_before_loading</a></span>    </div>
    -->

    
  </div>
  
  
  <div class="page">
    <!-- wikipage start -->
    <!-- TOC START -->
<div class="toc">
<div class="tocheader toctoggle" id="toc__header">Table of Contents</div>
<div id="toc__inside">

<ul class="toc">
<li class="clear">

<ul class="toc">
<li class="level2"><div class="li"><span class="li"><a href="#formatting_changing_data_before_loading" class="toc">Formatting/Changing Data before Loading</a></span></div>
<ul class="toc">
<li class="level3"><div class="li"><span class="li"><a href="#base_formatting_methods" class="toc">Base Formatting Methods</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#formatting_in_combo" class="toc">Formatting in Combo</a></span></div></li>
<li class="level3"><div class="li"><span class="li"><a href="#formatting_in_treegrid_and_tree" class="toc">Formatting in TreeGrid and Tree</a></span></div></li></ul>
</li></ul>
</li></ul>
</div>
</div>
<!-- TOC END -->

<p>
<div class='backlinks'><div class='backlink last_backlink'><img src="icons___file.gif"  class="media" alt="" /><a href="../index.html" class="wikilink1" title="start">Index</a></div><div class='arrow'></div><div class='backlink last_backlink'><img src="icons___connector.gif"  class="media" alt="" /><a href="index.html" class="wikilink1" title="dhtmlxconnector:toc">dhtmlxconnector</a></div><div class='arrow'></div><div class='backlink'><span class="curid"><a href="dhtmlxconnector___formatting_changing_data_before_loading.html" class="wikilink1" title="dhtmlxconnector:formatting_changing_data_before_loading">  Formatting/Changing Data before Loading  </a></span></div></div>

</p>

<h2><a name="formatting_changing_data_before_loading" id="formatting_changing_data_before_loading">Formatting/Changing Data before Loading</a></h2>
<div class="level2">

</div>

<h3><a name="base_formatting_methods" id="base_formatting_methods">Base Formatting Methods</a></h3>
<div class="level3">

<p>

In case you need to update values which were returned from database table or set some specific formatting before sending them to client side, you can use the “beforeRender” event handler. 
</p>

<p>
Common use-case will be similar to next

</p>
<pre class="code php">	<span class="re0">$res</span><span class="sy0">=</span><a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="re0">$mysql_server</span><span class="sy0">,</span><span class="re0">$mysql_user</span><span class="sy0">,</span><span class="re0">$mysql_pass</span><span class="br0">&#41;</span>;
	<a href="http://www.php.net/mysql_select_db"><span class="kw3">mysql_select_db</span></a><span class="br0">&#40;</span><span class="re0">$mysql_db</span><span class="br0">&#41;</span>;
	<span class="kw1">require</span><span class="br0">&#40;</span><span class="st0">&quot;../../codebase/grid_connector.php&quot;</span><span class="br0">&#41;</span>;	
&nbsp;
	<span class="kw2">function</span> color_rows<span class="br0">&#40;</span><span class="re0">$row</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
		<span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">get_index</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">%</span><span class="nu19">2</span><span class="br0">&#41;</span>
				<span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">set_row_color</span><span class="br0">&#40;</span><span class="st0">&quot;red&quot;</span><span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
&nbsp;
	<span class="re0">$grid</span> <span class="sy0">=</span> <span class="kw2">new</span> GridConnector<span class="br0">&#40;</span><span class="re0">$res</span><span class="br0">&#41;</span>;
	<span class="re0">$grid</span><span class="sy0">-&gt;</span><span class="me1">event</span><span class="sy0">-&gt;</span><span class="me1">attach</span><span class="br0">&#40;</span><span class="st0">&quot;beforeRender&quot;</span><span class="sy0">,</span><span class="st0">&quot;color_rows&quot;</span><span class="br0">&#41;</span>;
	<span class="re0">$grid</span><span class="sy0">-&gt;</span><span class="me1">render_table</span><span class="br0">&#40;</span><span class="st0">&quot;grid50000&quot;</span><span class="sy0">,</span><span class="st0">&quot;item_id&quot;</span><span class="sy0">,</span><span class="st0">&quot;item_nm,item_cd&quot;</span><span class="br0">&#41;</span>;</pre>
<p>

Here , color_rows function attached to the beforeRender event and set colors for rows based on their index.
</p>

<p>
During data generation, for each record outputed for client side beforeRender event will be executed, which means <em>color_rows</em> function will be called for each record. <strong>$row</strong> is an instance of GridDataItem object, related to current record.
</p>

<p>
Same approach can be used for data formating
</p>
<pre class="code php">	<span class="kw2">function</span> formatting<span class="br0">&#40;</span><span class="re0">$row</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
                  <span class="co1">//render field as details link</span>
                  <span class="re0">$data</span> <span class="sy0">=</span> <span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">get_value</span><span class="br0">&#40;</span><span class="st0">&quot;some_field&quot;</span><span class="br0">&#41;</span>;
                  <span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">set_value</span><span class="br0">&#40;</span><span class="st0">&quot;some_field&quot;</span><span class="sy0">,</span><span class="st0">&quot;&lt;a href='details.php?id={$data}'&gt;Details&lt;/a&gt;&quot;</span><span class="br0">&#41;</span>
&nbsp;
                  <span class="co1">//formatting date field</span>
                  <span class="re0">$data</span> <span class="sy0">=</span> <span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">get_value</span><span class="br0">&#40;</span><span class="st0">&quot;other_field&quot;</span><span class="br0">&#41;</span>;
                  <span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">set_value</span><span class="br0">&#40;</span><span class="st0">&quot;other_field&quot;</span><span class="sy0">,</span><a href="http://www.php.net/date"><span class="kw3">date</span></a><span class="br0">&#40;</span><span class="st0">&quot;m-d-Y&quot;</span><span class="sy0">,</span><a href="http://www.php.net/strtotime"><span class="kw3">strtotime</span></a><span class="br0">&#40;</span><span class="re0">$data</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
&nbsp;
	<span class="re0">$grid</span> <span class="sy0">=</span> <span class="kw2">new</span> GridConnector<span class="br0">&#40;</span><span class="re0">$res</span><span class="br0">&#41;</span>;
	<span class="re0">$grid</span><span class="sy0">-&gt;</span><span class="me1">event</span><span class="sy0">-&gt;</span><span class="me1">attach</span><span class="br0">&#40;</span><span class="st0">&quot;beforeRender&quot;</span><span class="sy0">,</span><span class="st0">&quot;formatting&quot;</span><span class="br0">&#41;</span>;</pre>
<p>
get_value and set_value method allow get or set value of any field related to the record ( it not affect actual values in DB ), if alias was used during data configuration - you need to use it instead of real db field name as first parameter of get|set command. 
</p>

<p>
More complex formating rules can be defined by using “extra” fields in configuration, fields which will not be outputed to client but can be used inside events. 

</p>
<pre class="code php">	<span class="kw2">function</span> formatting<span class="br0">&#40;</span><span class="re0">$row</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
                  <span class="co1">//set row color</span>
                  <span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">set_row_color</span><span class="br0">&#40;</span><span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">get_value</span><span class="br0">&#40;</span><span class="st0">&quot;color&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
                  <span class="co1">//save in userdata </span>
                  <span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">set_user_data</span><span class="br0">&#40;</span><span class="st0">&quot;some_data&quot;</span><span class="sy0">,</span><span class="re0">$row</span><span class="sy0">-&gt;</span><span class="me1">get_value</span><span class="br0">&#40;</span><span class="st0">&quot;count&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
&nbsp;
	<span class="re0">$grid</span> <span class="sy0">=</span> <span class="kw2">new</span> GridConnector<span class="br0">&#40;</span><span class="re0">$res</span><span class="br0">&#41;</span>;
	<span class="re0">$grid</span><span class="sy0">-&gt;</span><span class="me1">event</span><span class="sy0">-&gt;</span><span class="me1">attach</span><span class="br0">&#40;</span><span class="st0">&quot;beforeRender&quot;</span><span class="sy0">,</span><span class="st0">&quot;formatting&quot;</span><span class="br0">&#41;</span>;
        <span class="re0">$grid</span><span class="sy0">-&gt;</span><span class="me1">render_table</span><span class="br0">&#40;</span><span class="st0">&quot;some_table&quot;</span><span class="sy0">,</span><span class="st0">&quot;id&quot;</span><span class="sy0">,</span><span class="st0">&quot;name,price&quot;</span><span class="sy0">,</span><span class="st0">&quot;color,count&quot;</span><span class="br0">&#41;</span>;</pre>
<p>

Here, field color not outputed to client side , but used to define property of row. During update|insert operation only name and price columns may be changed, color will stay untouched.
Count field will be sent to client side as userdata of the row ( and it will be possible to access it on client side through related data. 
</p>

</div>

<h3><a name="formatting_in_combo" id="formatting_in_combo">Formatting in Combo</a></h3>
<div class="level3">

<p>
In case of dhtmlxCombo event works the same but provides ComboDataItem as parameter of event, it possible to implement the same use-cases with it
</p>

</div>

<h3><a name="formatting_in_treegrid_and_tree" id="formatting_in_treegrid_and_tree">Formatting in TreeGrid and Tree</a></h3>
<div class="level3">

<p>
TreeGrid provides TreeGridDataItem and Tree provides TreeDataItem for beforeUpdate event, both of them support base operations and few specific ones.
</p>
<pre class="code php"><span class="kw2">function</span> custom_format<span class="br0">&#40;</span><span class="re0">$item</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
                <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$item</span><span class="sy0">-&gt;</span><span class="me1">get_value</span><span class="br0">&#40;</span><span class="st0">&quot;complete&quot;</span><span class="br0">&#41;</span><span class="sy0">&gt;</span><span class="nu0">75</span><span class="br0">&#41;</span> 
		        <span class="re0">$item</span><span class="sy0">-&gt;</span><span class="me1">set_check_state</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>;
&nbsp;
		<span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$item</span><span class="sy0">-&gt;</span><span class="me1">get_value</span><span class="br0">&#40;</span><span class="st0">&quot;duration&quot;</span><span class="br0">&#41;</span><span class="sy0">&gt;</span><span class="nu0">10</span><span class="br0">&#41;</span>
			<span class="re0">$item</span><span class="sy0">-&gt;</span><span class="me1">set_image</span><span class="br0">&#40;</span><span class="st0">&quot;true.gif&quot;</span><span class="br0">&#41;</span>;
		<span class="kw1">else</span>
			<span class="re0">$item</span><span class="sy0">-&gt;</span><span class="me1">set_image</span><span class="br0">&#40;</span><span class="st0">&quot;false.gif&quot;</span><span class="br0">&#41;</span>;
	<span class="br0">&#125;</span>
	<span class="re0">$tree</span><span class="sy0">-&gt;</span><span class="me1">event</span><span class="sy0">-&gt;</span><span class="me1">attach</span><span class="br0">&#40;</span><span class="st0">&quot;beforeRender&quot;</span><span class="sy0">,</span><span class="st0">&quot;custom_format&quot;</span><span class="br0">&#41;</span>;</pre><ul>
<li class="level1"><div class="li"> set_image method allows to set image of tree element ( for treegrid it accepts only one parameter, while for tree it can be a single image of 3 different images for 3 states of tree&#039;s item. </div>
</li>
<li class="level1"><div class="li"> set_check method exists only in TreeDataItem object and allow to set state of related checkbox ( tree need to have checkboxes enabled in js. configuration code as well ) </div>
</li>
</ul>

<p>

beforeRender event can be used in dynamic Tree and TreeGrid to define which elements of hierarchy are branches and which are leafs. It is covered in related section of document. 

</p>

</div>

    <!-- wikipage stop -->
  </div>

  <div class="clearer">&nbsp;</div>

  
  <div class="stylefoot">

    <div class="meta">
      <div class="user">
              </div>
     <!-- <div class="doc">
        dhtmlxconnector/formatting_changing_data_before_loading.txt &middot; Last modified: 2009/07/30 12:56 (external edit)      </div>
    </div>-->

   
    <!--<div class="bar" id="bar__bottom">-->
      <!--<div class="bar-left" id="bar__bottomleft">-->
        <!---->
        <!--<form class="button btn_revs" method="get" action="http://dhtmlx.com/dhxdocs/doku.php"><div class="no"><input type="hidden" name="do" value="revisions" /><input type="hidden" name="id" value="dhtmlxconnector:formatting_changing_data_before_loading" /><input type="submit" value="Old revisions" class="button" accesskey="o" title="Old revisions [O]" /></div></form>-->
      <!--</div>-->
      <!--<div class="bar-right" id="bar__bottomright">-->
        <!---->
        <!---->
        <!---->
        <!---->
        <!--<form class="button btn_login" method="get" action="http://dhtmlx.com/dhxdocs/doku.php"><div class="no"><input type="hidden" name="do" value="login" /><input type="hidden" name="sectok" value="8a6ecdc39809afc74a409f6715c42728" /><input type="hidden" name="id" value="dhtmlxconnector:formatting_changing_data_before_loading" /><input type="submit" value="Login" class="button" title="Login" /></div></form>-->
        <!--<form class="button btn_index" method="get" action="http://dhtmlx.com/dhxdocs/doku.php"><div class="no"><input type="hidden" name="do" value="index" /><input type="hidden" name="id" value="dhtmlxconnector:formatting_changing_data_before_loading" /><input type="submit" value="Index" class="button" accesskey="x" title="Index [X]" /></div></form>-->
        <!--<a class="nolink" href="#dokuwiki__top"><input type="button" class="button" value="Back to top" onclick="window.scrollTo(0, 0)" title="Back to top" /></a>&nbsp;-->
      <!--</div>-->
      <!--<div class="clearer"></div>-->
    <!--</div>-->

  </div>

  

</div>

<div class="footerinc">


</div>

<div class="no"><img src="/dhtmlx/_lab/wiki/lib/exe/indexer.php?id=dhtmlxconnector%3Aformatting_changing_data_before_loading&amp;1257950912" width="1" height="1" alt=""  /></div>
     <div class="footer">
        <div class="footer-logo"></div>
        <div class="copyright">Copyright © 1998-2009 DHTMLX LTD.<br />All rights reserved.</div>
            	    </div>
</body>
</html>
